<html>
<head><meta charset="utf-8"><title>Link with --whole-archive · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html">Link with --whole-archive</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208160840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/208160840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Troy Patrick <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#208160840">(Aug 26 2020 at 23:10)</a>:</h4>
<p>I'm trying to link a static lib with a bunch of <code>.a</code> archives i get from a c project. the linker is removing a bunch of symbols i need due to how the interface is designed. if i was invoking <code>gcc</code> i could pass <code>-Wl,--whole-archive &lt;lib.a ...&gt; -Wl,--no-whole-archive</code> and it should work. anyone know the best way to achieve the same thing from a <code>build.rs</code>?</p>



<a name="208161380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/208161380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Troy Patrick <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#208161380">(Aug 26 2020 at 23:17)</a>:</h4>
<p>i have a <code>workspace</code> with <code>crates</code> in <code>./crates/*</code>. the lib that links against the <code>.a</code> files is one of the crates.</p>



<a name="208161492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/208161492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Troy Patrick <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#208161492">(Aug 26 2020 at 23:18)</a>:</h4>
<p>i have tried <code>println!("cargo:rustc-cdylib-link-arg=whole-archive")</code> in the <code>build.rs</code> but no joy (and actually im not sure on the form the flag needs to take ie if it needs the leading <code>--</code> or <code>-Wl</code>)</p>



<a name="208161616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/208161616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Troy Patrick <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#208161616">(Aug 26 2020 at 23:20)</a>:</h4>
<p>ive also tried adding the following to the <code>.cargo/config</code> in both the workspace root and crate root:</p>
<div class="codehilite"><pre><span></span><code>[build]
rustflags=[&quot;-C&quot;, &quot;link-args=-Wl,--whole-archive&quot;]
</code></pre></div>


<p>I can see the <code>cc</code> and <code>ld</code> invocations with strace but they dont appear to have the <code>--whole-archive</code> flag being passed in</p>



<a name="208161861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/208161861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Troy Patrick <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#208161861">(Aug 26 2020 at 23:23)</a>:</h4>
<p>there's also a RFC about this kind of linkage here: <a href="https://github.com/rust-lang/rfcs/pull/2951">https://github.com/rust-lang/rfcs/pull/2951</a></p>



<a name="208944970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/208944970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#208944970">(Sep 03 2020 at 10:04)</a>:</h4>
<p>Did you solve this? I'm intrigued to know if it can be done.</p>



<a name="209025094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/209025094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#209025094">(Sep 03 2020 at 20:26)</a>:</h4>
<p>The workaround that is available to wide class of issues like this is to tell rustc to produce a staticlib, and then link manually with whatever flags are necessary.</p>



<a name="209025231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Link%20with%20--whole-archive/near/209025231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Link.20with.20--whole-archive.html#209025231">(Sep 03 2020 at 20:27)</a>:</h4>
<p>Unfortunately, until <a href="https://github.com/rust-lang/rfcs/pull/2951">https://github.com/rust-lang/rfcs/pull/2951</a> is implemented, there's not way to wrap a specific native library into <code>-Wl,--whole-archive</code>/<code>-Wl,--no-whole-archive</code> from rustc.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>